import path from 'node:path';
import { ConfigEnv, loadEnv, UserConfig } from 'vite';
import vue from '@vitejs/plugin-vue';

const CWD = process.cwd();

// https://vitejs.dev/config/
export default ({ mode }: ConfigEnv): UserConfig => {
  const { VITE_BASE_URL, VITE_API_URL_PREFIX } = loadEnv(mode, CWD);

  return {
    base:VITE_BASE_URL,
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src'),
      },
    },

    css: {
      preprocessorOptions: {
        less: {
          modifyVars: {
            '@brand-color': '#0052d9',
            '@warning-color': '#ed7b2f',
            '@error-color': '#d54941',
            '@success-color': '#00a870',
          },
          javascriptEnabled: true,
        },
      },
    },

    plugins: [vue()],

    server: {
      host: '0.0.0.0',
      port: 3001,
      open: true,
      proxy: {
        [VITE_API_URL_PREFIX]: 'http://127.0.0.1:39000/',
      },
    },

    build: {
      outDir: 'dist',
      assetsDir: 'assets',
      sourcemap: false,
      rollupOptions: {
        output: {
          chunkFileNames: 'js/[name]-[hash].js',
          entryFileNames: 'js/[name]-[hash].js',
          assetFileNames: '[ext]/[name]-[hash].[ext]',
        },
      },
    },
  };
};